# -*- coding: utf-8 -*-#

# -------------------------------------------------------------------------------
# Name:         job51_utils
# Description:  提取51job页面的内容
# Author:       zhuchunxu
# Date:         2020/11/3
# -------------------------------------------------------------------------------
import datetime
import re
from logging import getLogger

from lxml import etree
from lxml.html import tostring

from spider_source.items import Job


class Scrc1Utils:

    def __init__(self):
        self.logger = getLogger(__name__)

    def get_page_detail_links(self, response):
        """
        获取当前页所有的详情页链接
        """
        html = etree.HTML(response.text)
        job_div_list = html.xpath("//div[@class='joblist']")
        cur_page_detail_link = []
        for div in job_div_list:
            a_list = div.xpath("./ul/li[1]/a")
            if len(a_list) > 0:
                href = "http://job.rc114.com/" + a_list[0].attrib.get("href")
                cur_page_detail_link.append(href)
        return cur_page_detail_link

    def get_page_detail_info(self, response):
        html = etree.HTML(response.text)
        job = Job()

        try:
            # 获取岗位名称
            job["title"] = html.xpath("//div[@class='TitileName']/text()")[0].strip()
        except Exception as e:
            self.logger.error('提取岗位名称错误')

        # 职位链接
        job["job_url"] = response.url
        job["from_site"] = "job.rc114.com"

        # 公司名称
        try:
            job["job_company"] =html.xpath("//div[@class='UnitBasciNameDiv']/a/text()")[0].strip()
            print(job["job_company"])
        except Exception as e:
            self.logger.error('提取公司名称错误')

        # 职位类别
        labela = html.xpath("//div[@class='JobItem']/ul/li[1]/a[1]/text()")[0].strip()
        labelb = html.xpath("//div[@class='JobItem']/ul/li[1]/a[2]/text()")[0].strip()
        job["category"] = labela+"-"+labelb


        if "兼职" in job["category"]:
            job["catalog"] = "兼职"
        else:
            job["catalog"] = "全职"

        # 职位描述
        try:
            job["job_desc"]=html.xpath("//div[@class='UnitInfoText JobInfo']")[0].xpath('string(.)').strip()
            # job["job_desc"] = tostring(
            #     html.xpath("//div[@class='UnitInfoText JobInfo']/text()")[0].strip(),
            #     encoding="utf-8").decode("utf-8")
        except Exception as e:
            self.logger.error('提取岗位内容错误:url:{},field:{}'.format(response.url, "job_desc"))


        address = html.xpath("//div[@class='JobItem']/ul/li[8]")[0].xpath('string(.)').strip()

        if "市" not in address:
           address =html.xpath("//div[@class='JobItem']/ul/li[9]")[0].xpath('string(.)').strip()



        if len(address) > 0:
            try:
                real_address=address.split("：")[1]
                if "市" in real_address:
                    job["city"] = real_address.split("市")[0].strip()+"市"
                    job["area"] = real_address.split("市")[1].split("区")[0].strip()+"区"
                else:
                    job["city"] = address[0].strip()
                    job["area"] = address[0].strip()
            except Exception as e:
                self.logger.error('提取城市、区域等内容错误')


            job["education"] = html.xpath("//div[@class='JobItem']/ul/li[3]")[0].xpath('string(.)').strip().split("：")[1]
            job["experience"] = html.xpath("//div[@class='JobItem']/ul/li[4]")[0].xpath('string(.)').strip().split("：")[1]
            job["recruit"] = html.xpath("//div[@class='JobItem']/ul/li[5]")[0].xpath('string(.)').strip().split("：")[1]
            job["date"]=html.xpath("//div[@class='JobItem']/ul/li[7]")[0].xpath('string(.)').strip().split("：")[1]

            # try:
            #     job["date"] = "{}-{}".format(datetime.datetime.now().year,job_date)
            # except Exception as e:
            #     self.logger.error('提取发布日期错误')

        try:

            salary = html.xpath("//div[@class='JobItem']/ul/li[2]")[0].xpath('string(.)').strip().split("：")[1]
            if len(salary) > 0:
                salary_range = re.findall(r"\d+\.?\d*", salary)

                if len(salary_range) > 0:
                    job["salary_min"] = salary_range[0]

                if len(salary_range) > 1:
                    job["salary_max"] = salary_range[1]

        except Exception as e:
            self.logger.error('提取薪资错误')

        return job
